package com.qk.controller;

import com.qk.anno.Log;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Role;
import com.qk.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RequestMapping("/roles")
@RestController
public class RoleController {
    @Autowired
    private RoleService roleService;
    //角色列表查询
    @GetMapping
    public Result list(String name, String label,String remark,
                       @RequestParam (defaultValue = "1") Integer page,
                       @RequestParam (defaultValue = "10") Integer pageSize){
        log.info("查询角色列表:"+name+","+label+","+remark+","+page+","+pageSize);
        PageResult<Role> pageResult = roleService.list(name,label,remark,page,pageSize);
        return Result.success(pageResult);
    }
    @Log
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("删除角色:"+id);
        roleService.delete(id);
        return Result.success();
    }
    @Log
    @PostMapping
    public Result add(@RequestBody Role role){
        log.info("新增角色:"+role);
        roleService.add(role);
        return Result.success();
    }
    //根据id查询
    @GetMapping("/{id}")
    public Result getRoleById(@PathVariable Integer id){
        log.info("查询角色:"+id);
        Role role = roleService.getRoleById(id);
        return Result.success(role);
    }
    @Log
    @PutMapping
    public Result updateRole(@RequestBody Role role){
        log.info("修改角色:"+role);
        roleService.updateRole(role);
        return Result.success();
    }
    @GetMapping("/list")
    public Result allList(){
            return Result.success(roleService.allList());
    }
}
